/**
*@项目名称: ecard-gprs-api
*@文件名称: MessageUtil.java
*@Date: 2018年10月9日
*@Copyright: 2018 www.abing.tech Inc. All rights reserved.
*/

package com.platform.util.wechat;

import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息转换工具类
 * @author qibing.huang
 * @date 2018年10月9日
 */
public class MessageUtil {

	/**
	 * xml转Map
	 * 
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static Map<String, String> xmlToMap(HttpServletRequest request) {
		Map<String, String> map = new HashMap<>();
		SAXReader reader = new SAXReader();
		ServletInputStream in = null;
		try {
			in = request.getInputStream();
			Document doc = reader.read(in);
			Element root = doc.getRootElement();
			List<Element> list = root.elements();
			for (Element e : list) {
				map.put(e.getName(), e.getText());
			}
			in.close();
		} catch (Exception e) {

		}
		return map;
	}

	/**
	 * 文本对象转换成xml
	 * 
	 * @param message
	 * @return
	 */
	public static String textMessageToXml(Object message) {
		XStream xStream = new XStream();
		xStream.alias("xml", message.getClass());
		return xStream.toXML(message);
	}

	/**
	 * 初始化文本信息
	 * 
	 * @param toUserName
	 * @param fromUserName
	 * @param msgType
	 * @param content
	 * @return
	 */
	public static String initText(String toUserName, String fromUserName, String msgType, String content) {
		TextMessage textMessage = new TextMessage();
		textMessage.setFromUserName(toUserName);
		textMessage.setToUserName(fromUserName);
		textMessage.setMsgType(msgType);
		textMessage.setCreateTime(System.currentTimeMillis());
		textMessage.setContent(content);
		return MessageUtil.textMessageToXml(textMessage);
	}

}
